<?php

namespace Data;

class UserPhoto extends Base {

	public function addPhoto($userId, $title, $desc, $path) {
		$data = array(
			'UserId' => $userId,
			'Title' => $title,
			'Desc' => $desc,
			'Path' => $path,
			'CreateTime' => time(),
		);
		$this->insert($data);
	}

	public function updatePhoto($userId, $photoId, $title, $desc) {
		$condition = array(
			'PhotoId' => $photoId,
			'UserId' => $userId,
		);
		$data = array(
			'Title' => $title,
			'Desc' => $desc,
		);
		$this->where($condition)->update($data);
	}

	public function getUserPhotosByUserId($userId, $limit = null) {
		$condition = array(
			'UserId' => $userId,
			'Status' => self::IS_YES,
		);
		$this->where($condition)->order('PhotoId desc');
		if ((int) $limit) {
			$this->limit((int) $limit);
		}
		$result = $this->findAll();
		return $result;
	}

	public function getDetailById($photoId) {
		$condition = array(
			'PhotoId' => $photoId,
			'Status' => self::IS_YES,
		);
		return $this->where($condition)->findOne();
	}

	public function delPhoto($userId, $photoId) {
		$condition = array(
			'PhotoId' => $photoId,
			'UserId' => $userId,
		);
		$this->where($condition)->del();
	}
}